<?php
/**
 * 全局配置文件
 */

// 站点基本信息
define('SITE_NAME', '男友事务预约');
define('SITE_URL', 'http://' . $_SERVER['HTTP_HOST']);
define('ROOT_PATH', dirname(__DIR__));
define('UPLOAD_PATH', ROOT_PATH . '/uploads');
define('AVATAR_PATH', UPLOAD_PATH . '/avatars');

// 时区设置
date_default_timezone_set('Asia/Shanghai');

// 会话设置
ini_set('session.cookie_httponly', 1);
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// 引入数据库配置
require_once 'database.php';

// 错误处理
function handleError($errno, $errstr, $errfile, $errline) {
    if (!(error_reporting() & $errno)) {
        return false;
    }
    
    $error = date('Y-m-d H:i:s') . ": [$errno] $errstr in $errfile on line $errline\n";
    error_log($error, 3, ROOT_PATH . '/error.log');
    
    if (error_reporting() == E_ALL) {
        echo "<b>Error:</b> [$errno] $errstr<br>";
        echo "Error on line $errline in $errfile<br>";
    } else {
        echo "<b>发生错误，请稍后再试。</b>";
    }
    
    return true;
}

set_error_handler('handleError');

// 通用函数
function redirect($url) {
    header('Location: ' . $url);
    exit;
}

function isLoggedIn() {
    return isset($_SESSION['user_id']);
}

function requireLogin() {
    if (!isLoggedIn()) {
        $_SESSION['error'] = '请先登录';
        redirect(SITE_URL . '/login.php');
    }
}

// 消息闪存
function setFlashMessage($type, $message) {
    $_SESSION['flash_messages'][$type] = $message;
}

function getFlashMessage() {
    if (isset($_SESSION['flash'])) {
        $flash = $_SESSION['flash'];
        unset($_SESSION['flash']);
        return $flash;
    }
    return null;
}
